//
//  ChangeLoginPwdViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/26/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
import SVProgressHUD

private struct kConstraints {
    
    static let headerHeight: CGFloat = xx_screenHeight() - 64
    static let headerRect: CGRect    = CGRectMake(0, 0, xx_screenWidth(), kConstraints.headerHeight)
    static let footerHeight: CGFloat = xx_screenHeight() - kConstraints.headerHeight - 64 - 49
    static let footerRect: CGRect    = CGRectMake(0, 0, xx_screenWidth(), kConstraints.footerHeight)
}

class ChangeLoginPwdViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func makeTableView() {
        
        tableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "tap"))
        
        tableView.tableHeaderView = changeLoginPwdView
        
        changeLoginPwdView.submitBtn.rac_signalForControlEvents(.TouchUpInside).subscribeNext { (_) -> Void in
            self.view.endEditing(true)
            
            xx_print(self.changeLoginPwdView.oldPwdTextField.text)
            xx_print(self.changeLoginPwdView.newPwdTextField.text)
            
            guard let oldPwd = self.changeLoginPwdView.oldPwdTextField.text else { return }
            guard let newPwd = self.changeLoginPwdView.newPwdTextField.text else { return }
            
            xx_showHUD(nil)
            AccountViewModel.shared.changeLoginPwd(oldPwd, newPwd: newPwd).subscribeError({ (error) -> Void in
                
                    xx_showError(error)
                
                }, completed: { () -> Void in
                    
                    xx_showInfo("修改成功")
                    xx_delay(1.2, task: { () -> () in
                        
                        self.navigationController?.popViewControllerAnimated(true)
                    })
            })
        }
    }

    func tap() {
        
        self.view.endEditing(true)
    }
    lazy var changeLoginPwdView = ChangeLoginPwdView(frame: kConstraints.headerRect)
    
}
